import sys
import time
from socket import *


class Update:
    def __init__(self, ip, prefix, out_port):
        self.ip = ip
        self.prefix = prefix
        self.out_port = out_port


def load(name):
    updates = []
    for line in open('i2/%s' % name):
        arr = line.split(' ')
        update = Update(arr[1], arr[2], arr[3].split('.')[0])
        updates.append(update)
    return updates


def send(updates):
    tcp_client_socket = socket(AF_INET, SOCK_STREAM)
    tcp_client_socket.settimeout(None)
    connected = False
    while True:
        if not connected:
            try:
                tcp_client_socket.connect(("0.0.0.0", 8888))
                connected = True
            except:
                pass
        else:
            msg = bytearray()
            s = time.time()
            for update in updates:
                msg.append(update.ip)

            tcp_client_socket.send(msg)

            d = time.time() - s
            print(d)


if __name__ == '__main__':
    updates = load(sys.argv[1] + 'notcomp')
    print(len(updates))
    send(updates)
